package org.eduapp.trains.utils;

import java.util.ArrayList;

public class UtilsAutoArrayList<T> extends ArrayList<T> {

	private static final long serialVersionUID = -8302380935425232908L;
	
	private Class<T> claz;
	
	public UtilsAutoArrayList(Class<T> claz){
		this.setClaz(claz);
	}
	
	public T get(int index){
		try{
			while(index >= size()){
				add(claz.newInstance());
			}
		}catch(Exception ex){
			ex.printStackTrace();
		}
		return super.get(index);
	}
	
	public T set(int index, T arg1)
	{
		try
		{
			while (index >= size())
			{
				add(claz.newInstance() );
			}
		}
		catch( Exception e )
		{
			e.printStackTrace();
		}
		return super.set(index, arg1);
	}
	
	public void setClaz(Class<T> claz) {
		this.claz = claz;
	}

	public Class<T> getClaz() {
		return claz;
	}

}
